﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using WpfApp1.chaxun;
using WpfApp1.Main;
using WpfApp1.Unit;
using WpfApp1.Zupan;
using WpfApp1.Contacts;
using WpfApp1.bean;
using System.Threading;
using System.Windows;

/**网络访问页面*/
namespace WpfApp1.Httpwork
{
    public class Http
    {
        
        //登录网络访问
        public async static void HttpDenglu(HttpDengluInterface dengluinterface , string content)
        {
            string url = contacts.HTTP+contacts.LOGIN;
            Console.WriteLine("url===================" + url);
            Boolean b = true;
            string result = "";
            HttpWebResponse resp = null;
            await Task.Run(() =>
            {


                HttpWebRequest req = postLoginHttp(url, content);
                
                try
                {
                    resp = (HttpWebResponse)req.GetResponse();
                    b = true;
                }
                catch (WebException ex)
                {
                    resp = (HttpWebResponse)ex.Response;
                    b = false;
                }
                Stream stream = resp.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
            });
            Console.WriteLine("result===================" + result);
            if (b)
            {
                dengluinterface.HttpDengluOk(result, resp);
            }
            else
            {
                dengluinterface.HttpDengluNo(result, b);

            }
            Console.WriteLine(result);
        }

        //根据包条码获取节包数据
        public async static void getHttp_Batch_Package(InterfacePackage interfacePackage, String pcode)
        {
            Boolean b = true;
            string result = "";
            await Task.Run(() =>
            {
                
                string url = contacts.HTTP + contacts.PCODE_BATCH_PACKAGE;
                data bean = new data();


                //bean.categorys = categorys;


                bean.pcode = pcode;


                string content = JsonConvert.SerializeObject(bean);
                
                Console.WriteLine(url);
                Console.WriteLine(content);

                HttpWebResponse resp = null;

                HttpWebRequest req = postHttp(url, content);
                req.ServicePoint.Expect100Continue = false;
                try
                {
                    resp = (HttpWebResponse)req.GetResponse();
                    b = true;
                }
                catch (WebException ex)
                {
                    resp = (HttpWebResponse)ex.Response;
                    b = false;
                }
                Stream stream = resp.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
                
            });
            if (b)
            {
                Console.WriteLine(result);
                interfacePackage.setBaoPackage(result, false, pcode);
            }
            else
            {
                Console.WriteLine(result);
                interfacePackage.setError(result);

            }

        }

        //获取包数据
        public async static void getHttpPackage(InterfacePackage interfacePackage, List<String> batchs )
        {
            Boolean b = true;
            string result = "";
            await Task.Run(() =>
            {
                
                string url = contacts.HTTP + contacts.BATCH_PACKAGE;
                data bean = new data();
                if (batchs != null)
                {
                    bean.batchs = batchs;
                }
                //if (categorys != null)
                //{
                //    bean.categorys = categorys;
                //}


                string content = JsonConvert.SerializeObject(bean);
                

                HttpWebResponse resp = null;

                HttpWebRequest req = postHttp(url, content);
                req.ServicePoint.Expect100Continue = false;
                try
                {
                    resp = (HttpWebResponse)req.GetResponse();
                    b = true;
                }
                catch (WebException ex)
                {
                    resp = (HttpWebResponse)ex.Response;
                    b = false;
                }
                Stream stream = resp.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
                
            });
            if (b)
            {

                interfacePackage.setPackage(result, true);
            }
            else
            {
                interfacePackage.setError(result);

            }

        }

        //根据托盘号查询托盘内容
        public async static void getPallectData(HttpGetPallectData interfacePackage,String pallect_num ,int pan)
        {
            Boolean b = true;
            string result = "";
            await Task.Run(() =>
            {
                var zu = new { pallet_num = pallect_num, pcode = "" };
                string content = JsonConvert.SerializeObject(zu);
                
                string url = contacts.HTTP + contacts.PALLET_DATA;
                
                HttpWebResponse resp = null;

                HttpWebRequest req = postHttp(url, content);
                req.ServicePoint.Expect100Continue = false;
                try
                {
                    resp = (HttpWebResponse)req.GetResponse();
                    b = true;
                }
                catch (WebException ex)
                {
                    resp = (HttpWebResponse)ex.Response;
                    b = false;
                }
                Stream stream = resp.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }

            });
            if (b)
            {

                interfacePackage.getPallectDataOk(result, pan);
            }
            else
            {
                interfacePackage.getPallectDataNo(result, b);

            }
        }
        //上传托盘组盘数据
        public async static void PostZupan(HttpPostZupanInterface httpPostZupanInterface, String content,String pan)
        {
            string result = "";
            Boolean b = true;
            await Task.Run(() =>
            {

                Console.WriteLine("content===================" + content);
                string url = contacts.HTTP + contacts.ZU_PALLET;
                Console.WriteLine("url===================" + url);
                
                HttpWebResponse resp = null;
                HttpWebRequest req = postHttp(url, content);
                req.ServicePoint.Expect100Continue = false;
                try
                {
                    resp = (HttpWebResponse)req.GetResponse();
                    b = true;
                }
                catch (WebException ex)
                {
                    resp = (HttpWebResponse)ex.Response;
                    b = false;
                }
                Stream stream = resp.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
                Console.WriteLine("=================" + result);
                

            });
            if (b)
            {
                httpPostZupanInterface.httpPostZupanOk(result, pan);
            }
            else
            {
                httpPostZupanInterface.httpPostZupanNo(result, b);

            }
        }

        //删除包组盘
        public async static void deleteZupan(HttpDeleteZupanInterface httpDeleteZupanInterface, packagebean bean)
        {
            Boolean b = true;
            string result = "";
            await Task.Run(() =>
            {
                
                string url = contacts.HTTP + contacts.DELETE_ZU_BACKAGE;
                delect_package delete_bean = new delect_package();
                List<String> pcodes = new List<String>();
                pcodes.Add(bean.pcode);
                delete_bean.pcodes = pcodes;

                string content = JsonConvert.SerializeObject(delete_bean);
                
                Console.WriteLine(url);
                Console.WriteLine(content);

                HttpWebResponse resp = null;

                HttpWebRequest req = postHttp(url, content);
                req.ServicePoint.Expect100Continue = false;
                try
                {
                    resp = (HttpWebResponse)req.GetResponse();
                    b = true;
                }
                catch (WebException ex)
                {
                    resp = (HttpWebResponse)ex.Response;
                    b = false;
                }
                Stream stream = resp.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
                Console.WriteLine("===================" + result);
                
            });
            if (b)
            {

                httpDeleteZupanInterface.httpDeleteZupanOk(bean);
            }
            else
            {
                httpDeleteZupanInterface.httpDeleteZupanNo(result, b);

            }
        }

        //完工入组盘区
        public async static void finish_zupan(InterfacePackage interfacePackage,String num)
        {

            Boolean b = true;
            string result = "";
            await Task.Run(() =>
            {
                
                
                string url = contacts.HTTP + contacts.FINISH_YIPAN;
                delect_package delete_bean = new delect_package();
                yipanBean bean = new yipanBean();
                bean.pallet_num = num;
                bean.buffer_entrance_status = 1;
                bean.buffer_finish_status = 0;
                yipanBean[] yipans = { bean };

                yipan_kuang yipan_content = new yipan_kuang();
                yipan_content.pallet_buffer = yipans;

                string content = JsonConvert.SerializeObject(yipan_content);
                

                HttpWebResponse resp = null;

                HttpWebRequest req = postHttp(url, content);
                req.ServicePoint.Expect100Continue = false;
                try
                {
                    resp = (HttpWebResponse)req.GetResponse();
                    b = true;
                }
                catch (WebException ex)
                {
                    resp = (HttpWebResponse)ex.Response;
                    b = false;
                }
                Stream stream = resp.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
                Console.WriteLine("===================" + result);
                
            });
            if (b)
            {
                interfacePackage.setFinishPackageOK(result);
            }
            else
            {
                interfacePackage.setFinishPackageNo(result);

            }
        }

        public static HttpWebRequest postLoginHttp(String url, string content)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json";
            req.Accept = "application/json";
            req.Headers.Add("X-CSRF-TOKEN", MainWindow.token);
            req.CookieContainer = MainWindow.cookiecon;
            #region 添加Post 参数
            byte[] data = Encoding.UTF8.GetBytes(content);
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            return req;
        }

        public static HttpWebRequest postHttp(String url, string content)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json";
            req.Accept = "application/json";
            req.Headers.Add("X-CSRF-TOKEN", MainWindow.token);
            req.CookieContainer = MainWindow.cookiecon;
            
            req.Headers.Add("COOKIE", MainWindow.COOKIE);
            Console.WriteLine(MainWindow.token);
            Console.WriteLine(MainWindow.COOKIE);
            #region 添加Post 参数
            byte[] data = Encoding.UTF8.GetBytes(content);
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            return req;
        }

        public static HttpWebRequest getHttp(String url)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "GET";
            req.CookieContainer = MainWindow.cookiecon;
            req.Headers.Add("COOKIE", MainWindow.COOKIE);
            #region 添加Post 参数
            #endregion
            return req;
        }

        

    }
}
